package com.fastwire.carriere.hibernate.daoimpl;

import java.util.List;

import org.hibernate.FetchMode;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.fastwire.carriere.common.util.StringUtil;
import com.fastwire.carriere.dao.CrossConnectDao;
import com.fastwire.carriere.dao.DAOException;
import com.fastwire.carriere.inventory.entity.CrossConnect;
import com.fastwire.carriere.inventory.vo.CrossConnectVo;

// TODO: Auto-generated Javadoc
/**
 * The Class CrossConnectDaoImpl.
 */
public class CrossConnectDaoImpl extends AbstractDAOImpl implements
		CrossConnectDao {

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.fastwire.carriere.dao.CrossConnectDao#saveOrUpdateCC(com.fastwire
	 * .carriere.inventory.entity.CrossConnect)
	 */
	@Override
	public void saveOrUpdateCC(CrossConnect crossConnect) throws DAOException {
		super.saveOrUpdate(crossConnect);
	}

	@Override
	public List<CrossConnect> findAllCCs() throws DAOException {
		DetachedCriteria criteria = DetachedCriteria
				.forClass(CrossConnect.class);
		criteria = criteria.add(getStatusCriteria());
		criteria.setFetchMode("transParameters", FetchMode.SELECT);
		criteria.setFetchMode("aendTp", FetchMode.SELECT);
		criteria.setFetchMode("zendTp", FetchMode.SELECT);
		return getHibernateTemplate().findObjsByCriteria(criteria, false, true);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.fastwire.carriere.dao.CrossConnectDao#getCrossConnectsbyServiceID
	 * (java.lang.String, java.lang.String)
	 */
	@Override
	public List getCrossConnectsbyServiceID(String serviceId, String neID)
			throws DAOException {

		serviceId = serviceId.replaceAll("SNC", "CC");
		serviceId = serviceId.replaceAll(";", "::");
		DetachedCriteria criteria = DetachedCriteria
				.forClass(CrossConnect.class);
		criteria.add(Restrictions.like("name", serviceId, MatchMode.ANYWHERE));
		if (!StringUtil.isEmptyString(neID)) {
			criteria.add(Restrictions.like("name", neID, MatchMode.ANYWHERE));
		}
		criteria = criteria.add(getStatusCriteria());
		return getHibernateTemplate().findObjsByCriteria(criteria, false, true);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.fastwire.carriere.dao.CrossConnectDao#getCrossConnectsbyName(java
	 * .lang.String)
	 */
	@Override
	public List getCrossConnectsbyName(String ccName) throws DAOException {
		DetachedCriteria criteria = DetachedCriteria
				.forClass(CrossConnect.class);
		//NATIVEemsnAME = AMPOC1.AMPOC1-AM448 -TES-PTN950-1-MPLS.Stati-791-00000676
		Criterion nameCriterion = Restrictions.eq("emsNativeName", ccName);
		Criterion nativeNameCriterion = Restrictions.like("name", ccName,
				MatchMode.ANYWHERE);
		criteria.add(Restrictions.or(nameCriterion, nativeNameCriterion));

		criteria = criteria.add(getStatusCriteria());
		// joinTransmissionParameter(criteria);

		return getHibernateTemplate().findObjsByCriteria(criteria, false, true);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.fastwire.carriere.dao.CrossConnectDao#
	 * getAllCrossConnectEmsNativeNamesbyNEName(java.lang.String)
	 */
	@Override
	public List getAllCrossConnectEmsNativeNamesbyNEName(String neID)
			throws DAOException {

		DetachedCriteria criteria = DetachedCriteria
				.forClass(CrossConnect.class);
		criteria = criteria.add(getStatusCriteria());

		if (!StringUtil.isEmptyString(neID)) {
			joinTp(criteria);
			Criterion aendCriterion = Restrictions.like("aendTp.name", neID,
					MatchMode.ANYWHERE);
			Criterion zendCriterion = Restrictions.like("zendTp.name", neID,
					MatchMode.ANYWHERE);

			criteria.add(Restrictions.or(aendCriterion, zendCriterion));
		}

		criteria.setProjection(Projections.distinct(Projections
				.property("emsNativeName")));

		return getHibernateTemplate().findObjsByCriteria(criteria, false, true);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.fastwire.carriere.dao.CrossConnectDao#getCrossConnectsbyNE(java.lang
	 * .String)
	 */
	@Override
	public List getCrossConnectsbyNE(String neID) throws DAOException {

		DetachedCriteria criteria = DetachedCriteria
				.forClass(CrossConnect.class);
		criteria = criteria.add(getStatusCriteria());
		joinTp(criteria);
		Criterion aendCriterion = Restrictions.like("aendTp.name", neID,
				MatchMode.ANYWHERE);
		Criterion zendCriterion = Restrictions.like("zendTp.name", neID,
				MatchMode.ANYWHERE);
		joinTransmissionParameter(criteria);
		criteria.add(Restrictions.or(aendCriterion, zendCriterion));
		List allCCs = null;
		allCCs=getHibernateTemplate().findObjsByCriteria(criteria, false, true);
		//allCCs=getHibernateTemplate().findObjsByCriteria(criteria, false, false);
		return allCCs;
	

	}
	
	@Override
	public List getCrossConnectsbyNE(String neID, boolean forceLoad) throws DAOException {

		DetachedCriteria criteria = DetachedCriteria
				.forClass(CrossConnect.class);
		criteria = criteria.add(getStatusCriteria());
		joinTp(criteria);
		Criterion aendCriterion = Restrictions.like("aendTp.name", neID,
				MatchMode.ANYWHERE);
		Criterion zendCriterion = Restrictions.like("zendTp.name", neID,
				MatchMode.ANYWHERE);
		joinTransmissionParameter(criteria);
		criteria.add(Restrictions.or(aendCriterion, zendCriterion));

		//return getHibernateTemplate().findObjsByCriteria(criteria, false, true);
		return getHibernateTemplate().findObjsByCriteria(criteria, false, forceLoad);

	}

	@Override
	public List getCrossConnectsbyPTPName(String ptpFullName)
			throws DAOException {

		DetachedCriteria criteria = DetachedCriteria
				.forClass(CrossConnect.class);
		criteria = criteria.add(getStatusCriteria());
		joinTp(criteria);
		Criterion aendCriterion = Restrictions.like("aendTp.name", ptpFullName,
				MatchMode.START);
		Criterion zendCriterion = Restrictions.like("zendTp.name", ptpFullName,
				MatchMode.START);
		criteria.add(Restrictions.or(aendCriterion, zendCriterion));

		return getHibernateTemplate()
				.findObjsByCriteria(criteria, false, false);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.fastwire.carriere.dao.CrossConnectDao#
	 * getCrossConnectsbyNativeEmsNameAndNEId(java.lang.String,
	 * java.lang.String)
	 */
	@Override
	public CrossConnect getCrossConnectsbyNativeEmsNameAndNEId(
			String emsNativeName, String neID) throws DAOException {

		DetachedCriteria criteria = DetachedCriteria
				.forClass(CrossConnect.class);
		criteria = criteria.add(getStatusCriteria());
		joinTp(criteria);
		Criterion aendCriterion = Restrictions.like("aendTp.name", neID,
				MatchMode.ANYWHERE);
		Criterion zendCriterion = Restrictions.like("zendTp.name", neID,
				MatchMode.ANYWHERE);
		criteria.add(Restrictions.or(aendCriterion, zendCriterion)).add(
				Restrictions.eq("emsNativeName", emsNativeName));

		return (CrossConnect) getHibernateTemplate().findSingleObjByCriteria(
				criteria, false, true);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.fastwire.carriere.dao.CrossConnectDao#
	 * getCrossConnectListsbyNEAndEmsnativeName(java.util.List,
	 * java.lang.String)
	 */
	@Override
	public List<CrossConnect> getCrossConnectListsbyNEAndEmsnativeName(
			List<String> emsNameList, String neID) throws DAOException {
		DetachedCriteria criteria = DetachedCriteria
				.forClass(CrossConnect.class);
		criteria = criteria.add(getStatusCriteria());
		joinTp(criteria);
		Criterion aendCriterion = Restrictions.like("aendTp.name", neID,
				MatchMode.ANYWHERE);
		Criterion zendCriterion = Restrictions.like("zendTp.name", neID,
				MatchMode.ANYWHERE);
		criteria.add(Restrictions.or(aendCriterion, zendCriterion));

		if (!emsNameList.isEmpty()) {

			criteria.add(Restrictions.in("emsNativeName", emsNameList));

		}

		return getHibernateTemplate().findObjsByCriteria(criteria, false, true);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.fastwire.carriere.dao.CrossConnectDao#
	 * getAllCrossConnectsNativeEMSNamebyServiceID(java.lang.String)
	 */
	@Override
	public List getAllCrossConnectsNativeEMSNamebyServiceID(String serviceID)
			throws DAOException {
		DetachedCriteria criteria = DetachedCriteria
				.forClass(CrossConnect.class);
		criteria = criteria.add(getStatusCriteria());
		criteria.add(Restrictions.like("emsNativeName", serviceID,
				MatchMode.ANYWHERE));

		criteria.setProjection(
				Projections.distinct(Projections.property("emsNativeName")))
				.addOrder(Order.desc("emsNativeName"));

		return getHibernateTemplate().findObjsByCriteria(criteria, false, true);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.fastwire.carriere.dao.CrossConnectDao#getCrossConnectsbyNEAndName
	 * (java.lang.String, java.lang.String)
	 */
	@Override
	public CrossConnect getCrossConnectsbyNEAndName(String name, String neID)
			throws DAOException {

		DetachedCriteria criteria = DetachedCriteria
				.forClass(CrossConnect.class);
		criteria = criteria.add(getStatusCriteria());
		joinTp(criteria);
		Criterion aendCriterion = Restrictions.like("aendTp.name", neID,
				MatchMode.ANYWHERE);
		Criterion zendCriterion = Restrictions.like("zendTp.name", neID,
				MatchMode.ANYWHERE);
		criteria.add(Restrictions.like("name", name, MatchMode.ANYWHERE));
		criteria.add(Restrictions.or(aendCriterion, zendCriterion));
		return (CrossConnect) getHibernateTemplate().findSingleObjByCriteria(
				criteria, false, true);

	}

	/**
	 * Join tp.
	 * 
	 * @param criteria
	 *            the criteria
	 */
	private void joinTp(DetachedCriteria criteria) {
		criteria.setFetchMode("aendTp", FetchMode.JOIN)
				.createAlias("aendTp", "aendTp")
				.setFetchMode("zendTp", FetchMode.JOIN)
				.createAlias("zendTp", "zendTp");
	}

	/**
	 * Join transmission parameter.
	 * 
	 * @param criteria
	 *            the criteria
	 */
	private void joinTransmissionParameter(DetachedCriteria criteria) {
		criteria.setFetchMode("transParameters", FetchMode.SELECT).createAlias(
				"transParameters", "tParameters");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.fastwire.carriere.dao.CrossConnectDao#getCrossConnectsbyVo(com.fastwire
	 * .carriere.inventory.vo.CrossConnectVo)
	 */
	@Override
	public List<String> getCrossConnectsbyVo(CrossConnectVo ccVo)
			throws DAOException {

		DetachedCriteria criteria = DetachedCriteria
				.forClass(CrossConnect.class);

		Criterion criterion = getStatusCriteria();

		if (!StringUtil.isEmptyString(ccVo.getNativeEmsName())) {
			criterion = Restrictions.and(
					criterion,
					getWildCardCriterion("emsNativeName",
							ccVo.getNativeEmsName()));

		}
		criteria.add(criterion);
		criteria.addOrder(Order.asc("emsNativeName"));
		criteria.setProjection(Projections.distinct(Projections
				.property("emsNativeName")));

		return getHibernateTemplate().findObjsByCriteria(criteria, false, true);
	}

	@Override
	public void invalidateAll() throws DAOException {
		DetachedCriteria criteria = DetachedCriteria
				.forClass(CrossConnect.class);
		criteria = criteria.add(getStatusCriteria());
		getHibernateTemplate().invalidateObjsByCriteria(criteria);

	}

}
